﻿using Dapper;
using HospitalFollowUpSystem.Entity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;

namespace HospitalFollowUpSystem.DBC
{
    public class HIS_DB
    {
        private String connStr;
        private SqlConnection conn;

        public SqlConnection Database { get => conn; }

        public HIS_DB()
        {
            connStr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            conn = new SqlConnection(connStr);
            //HI 记得随时释放连接 db.Database.close();
            conn.Open();
        }

        /// <summary>
        /// 获得 某个时间区间的 出院病人
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public List<OutPatient> ReadDischargedPatient(DateTime startDate, DateTime endDate)
        {
            String sqlStr = @"SELECT  PatientID AS id ,
                                      M04 AS name ,
                                      M05 AS sex ,
                                      M06 AS birthday ,
                                      M08 AS idCard ,
                                      M09 AS telNum ,
                                      M10 AS nowAddress ,
                                      M11 AS inDate ,
                                      M12 AS  inKSCode,
                                      M13 AS  inKSName,
                                      M19 AS outDate ,
                                      M20 AS outKSCode ,
                                      M21 AS outKSName
                              FROM    dbo.MBASE
                              WHERE   M19 IS NOT NULL
                                      AND M19 >= @startDate
                                      AND M19 < @endDate";
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@startDate", startDate);
            sqlParam.Add("@endDate", endDate);
            var res = conn.Query<OutPatient>(sqlStr, sqlParam).ToList();
            return res;
        }

        
    }
}
